package ioStream.file.processStream.characterStream;

import java.io.*;

/**
 * Created with IntelliJ IDEA
 * Description:转换输出流学习
 * User:22954
 * Date:2024-03-26
 * Time:1:11
 */
@SuppressWarnings("all")
public class OutputStreamWriter_ {
    /*
    OutputStreamWriter extends Writer
    OutputStreamWriter(OutputStream out, String charsetName)  可以将字节流包装(转换)成为字符流,并指定读取的编码格式
    在写入纯文本数据时,通过转换成字符流可以提高效率,并指定编码格式
     */
    public static void main(String[] args) {
        String filePath = "f:/Test/NewFile4.txt";
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            /*
            一般将FileOutputStream包装/转换成OutputStreamWriter,进而包装/转换成BufferedWriter
             */
            osw = new OutputStreamWriter(new FileOutputStream(filePath),"gbk");
            bw = new BufferedWriter(osw);
            bw.write("hello 你好呀！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                bw.close();//关闭外层流的同时,系统自动关闭所有内层流
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
